//index.js
const app = getApp()
const db = wx.cloud.database()

Page({
  data: {
    markers: [{
      iconPath: "/images/位置.png",
      id: 0,
      latitude: 23.632575,
      longitude: 113.679287,
      width: 40,
      height: 40
    }],
    startplace: '我的位置',
    endplace: '您要去哪'
  },

//出发地选
startplace: function() {
  var that = this;
  wx.chooseLocation({
    success: function(res) {
      console.log(res)
      var startlatitude = res.latitude;
      var startlongitude = res.longitude;
      var orgin = startlongitude + ',' + startlatitude
      wx.setStorageSync('orgin', orgin)
      that.setData({
        startplace: res.name,
        startcolor: '#000',
        startlatitude: startlatitude,
        startlongitude: startlongitude
      })
    },
    fail:function(){
      wx.getSetting({
        success: function (res) {
          var statu = res.authSetting;
          if (!statu['scope.userLocation']) {
            wx.showModal({
              title: '是否授权当前位置',
              content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
              success: function (tip) {
                if (tip.confirm) {
                  wx.openSetting({
                    success: function (data) {
                      if (data.authSetting["scope.userLocation"] === true) {
                        wx.showToast({
                          title: '授权成功',
                          icon: 'success',
                          duration: 1000
                        })
                        //授权成功之后，再调用chooseLocation选择地方
                        wx.chooseLocation({
                          success: function (res) {
                            obj.setData({
                              addr: res.address
                            })
                          },
                        })
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'success',
                          duration: 1000
                        })
                      }
                    }
                  })
                }
              }
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '调用授权窗口失败',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }
  })        
    
},
endplace: function() {
  var that = this;
  wx.chooseLocation({
    success: function(res) {
      var endlatitude = res.latitude;
      var endlongitude = res.longitude;
      var destination = endlongitude + ',' + endlatitude
      wx.setStorageSync('destination', destination)
      that.data.endlongitude = endlongitude;
      that.data.endlatitude = endlatitude;
      that.setData({
        endplace: res.name,
        endcolor: '#000',
        endlatitude: endlatitude,
        endlongitude: endlongitude
      });
    that.getdistance();
    },
  })
},
bindRegionChanges: function(e) {
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    regionstart: e.detail.value
  })
},
bindRegionChangek: function(e) {
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    regionend: e.detail.value
  })
},
bindDateChangestart: function(e) {
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    startdate: e.detail.value
  })
},
bindDateChangeend: function(e) {
  console.log('picker发送选择改变，携带值为', e.detail.value)
  this.setData({
    enddate: e.detail.value
  })
},

find:function(){
  wx.navigateTo({
    url:"../nearby/nearby"
  })
},







  onLoad: function() {
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  onGetUserInfo: function(e) {
    if (!this.data.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  onGetOpenid: function() {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
        wx.navigateTo({
          url: '../userConsole/userConsole',
        })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        wx.navigateTo({
          url: '../deployFunctions/deployFunctions',
        })
      }
    })
  },

  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            
            wx.navigateTo({
              url: '../storageConsole/storageConsole'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },

})
